package com.defysoft.waukids.main;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.test.suitebuilder.annotation.Smoke;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.R;

public class Main_JoinYacguan extends BaseActivity implements OnClickListener {
	Toolbar toolbar;
	TextView tv[] = new TextView[6];
	TextView iv[] = new TextView[3];
	Button btn;
	String regId;
	ScrollView sv[] = new ScrollView[3];
	ScrollView mainScrollView;
	CheckBox[] box = new CheckBox[3];
	int check_1 = 0, check_2 = 0, check_3 = 0;
	private final int SUCCESS = 10;
	private final int FAIL = 9;
	private final int NETWORK_ERR = 8;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_joinyacguan);
		setStartData();
		setStartToolbar();
		startNetwork(1);
	}

	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@Override
	public void setStartData() {
		super.setStartData();
		regId = getIntent().getStringExtra("MEM_UUID");
		
		mainScrollView = (ScrollView)findViewById(R.id.MAIN_YACGUAN_MAIN_SCROLLVIEW);
		toolbar = (Toolbar)findViewById(R.id.MAIN_YACGUAN_TOOLBAR);
		
		sv[0] = (ScrollView)findViewById(R.id.YACGUAN_FIRST_SCROLL);
		sv[1] = (ScrollView)findViewById(R.id.YACGUAN_SECOND_SCROLL);
		sv[2] = (ScrollView)findViewById(R.id.YACGUAN_THIRD_SCROLL);
		
		tv[0] = (TextView) findViewById(R.id.YACGUAN_FIRST_NAME);
		tv[1] = (TextView) findViewById(R.id.YACGUAN_FIRST_SUB);
		tv[2] = (TextView) findViewById(R.id.YACGUAN_SECOND_NAME);
		tv[3] = (TextView) findViewById(R.id.YACGUAN_SECOND_SUB);
		tv[4] = (TextView) findViewById(R.id.YACGUAN_THIRD_NAME);
		tv[5] = (TextView) findViewById(R.id.YACGUAN_THIRD_SUB);

		iv[0] = (TextView) findViewById(R.id.YACGUAN_FIRST_CHECK);
		iv[1] = (TextView) findViewById(R.id.YACGUAN_SECOND_CHECK);
		iv[2] = (TextView) findViewById(R.id.YACGUAN_THIRD_CHECK);
		
		box[0] = (CheckBox)findViewById(R.id.YACGUAN_FIRST_CHECKBOX);
		box[1] = (CheckBox)findViewById(R.id.YACGUAN_SECOND_CHECKBOX);
		box[2] = (CheckBox)findViewById(R.id.YACGUAN_THIRD_CHECKBOX);
		
		btn = (Button) findViewById(R.id.YACGUAN_SUCCESS);
		
		for (int i = 0; i < box.length; i++) {
			box[i].setOnClickListener(this);
		}
		
		sv[0].setOnTouchListener(new OnTouchListener() {
	        @Override
	        public boolean onTouch(View v, MotionEvent ev) {
	            v.getParent().requestDisallowInterceptTouchEvent(true);
	            return false;
	        }
		});

		sv[1].setOnTouchListener(new OnTouchListener() {
	        @Override
	        public boolean onTouch(View v, MotionEvent ev) {
	            v.getParent().requestDisallowInterceptTouchEvent(true);
	            return false;
	        }
		});
		sv[2].setOnTouchListener(new OnTouchListener() {
	        @Override
	        public boolean onTouch(View v, MotionEvent ev) {
	            v.getParent().requestDisallowInterceptTouchEvent(true);
	            return false;
	        }
		});
		
		btn.setOnClickListener(this);
	}

	// 약관 받아오기.
	void yacguanlist() {
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_JOIN_YACGUAN));

			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS, jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, "main_" + e.toString());
		}
	}

	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {

			@Override
			public void run() {
				switch (cho) {
				case 1:
					yacguanlist();
					break;
				}
			}
		});
		firstThread.start();
		startDIal();
	}

	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);

		switch (_message.what) {
		case SUCCESS:
			endDIal();
			JSONObject yacguan;
			try {
				yacguan = new JSONObject((String) _message.obj);
				tv[0].setText(yacguan.getString("First_Name"));
				tv[1].setText(yacguan.getString("First_Sub"));
				tv[2].setText(yacguan.getString("Two_Name"));
				tv[3].setText(yacguan.getString("Two_Sub"));
				tv[4].setText(yacguan.getString("Three_Name"));
				tv[5].setText(yacguan.getString("Three_Sub"));
			} catch (Exception e) {
			}
			break;

		case FAIL:
			endDIal();
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;

		case NETWORK_ERR:
			endDIal();
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;
		}
	}

	// 약관 동의 여부 체크
	void check_yac() {
		if (check_1 != 0 && check_2 != 0 && check_3 != 0){
			Intent i = new Intent(this,Main_JoinMember.class);
			i.putExtra("MEM_UUID", regId);
			startActivity(i);
			finish();
		}else{
			Toast.makeText(this, "위 사항에 모두 동의 하지 않으면 회원가입을 할 수 없습니다.", Toast.LENGTH_SHORT).show();
			if(check_1==0){
				iv[0].setTextColor(0xffFF0000);
			}else if (check_2==0){
				iv[1].setTextColor(0xffFF0000);
			}else{
				iv[2].setTextColor(0xffFF0000);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.YACGUAN_FIRST_CHECKBOX:
			if(box[0].isChecked()){
				iv[0].setTextColor(0xff000000);
				check_1 =1;
			}else{
				
				check_1 =0;
			}
			break;

		case R.id.YACGUAN_SECOND_CHECKBOX:
			if(box[1].isChecked()){
				iv[1].setTextColor(0xff000000);
				check_2 =1;
			}else{
				
				check_2 =0;
			}
			break;
		case R.id.YACGUAN_THIRD_CHECKBOX:
			if(box[2].isChecked()){
				iv[2].setTextColor(0xff000000);
				check_3 =1;
			}else{
				
				check_3 =0;
			}
			break;
		case R.id.YACGUAN_SUCCESS:
			check_yac();
			
			break;
		}

	}
}
